import { post, resetTokenFactor } from '../common/api'
import { ReadCache } from '../mox/read_cache'
import CryptoJS from 'crypto-js'


const userCache = new ReadCache(function(callback=function(data){}) {
  post('/user/profile', {}).then(function(res) {
    callback(res)
  })
})


export function getUser(callback=function(user){}) {
  userCache.get(function(user) {
    callback(user)
  })
}


export function resetUser() {
  userCache.reset()
}

export function logoutUser() {
  resetTokenFactor()
  resetUser()
}


export function hashPassword(origin) {
  var md5 = CryptoJS.algo.MD5.create()
  md5.update(origin)
  return md5.finalize().toString()
}


// export function assignUser(newUser) {
//   for (var key in user) {
//     if (newUser[key] != undefined) {
//       user[key] = newUser[key]
//     }
//   }
// }


// export function updateUser() {
//   return new Promise(function(resolve) {
//     post('/user/profile', {}).then(function(res) {
//       assignUser(res)
//       resolve(user)
//     })
//   })
// }
